package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/7/21.<br><br>
 *
 * 统计有序矩阵中的负数
 */
public class Topic1351 {

    public static void main(String[] args) {
        Topic1351 instance = new Topic1351();

        int[][] matrix = new int[][] {{4,3,2,-1},{3,2,1,-1},{1,1,-1,-2},{-1,-1,-2,-3}};
        System.out.println(instance.countNegatives(matrix));
    }

    public int countNegatives(int[][] grid) {
        int counter = 0;

        for(int i = grid.length - 1; i >= 0; i--) {
            for(int j = grid[i].length - 1; j >= 0; j--) {
                if(grid[i][j] < 0) {
                    counter++;
                }
                else {
                    break;
                }
            }
        }

        return counter;
    }
}
